//
//  PraiseListViewController.m
//  Cunli
//
//  Created by 湛亚 on 16/3/3.
//  Copyright © 2016年 lilubin. All rights reserved.
//

#import "PraiseListViewController.h"
#import "UIColor+Addition.h"
#import "PeopleDataViewController.h"
#import "UIImageView+WebCache.h"
#import "LinLi.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "MJRefresh.h"

@interface PraiseListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView *praiseListTableView;
    int contentPage;
    NSMutableArray *praiseListArray;
}

@end

@implementation PraiseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    contentPage=1;
    self.title=@"赞赏的人";
    self.view.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
    praiseListArray=[[NSMutableArray alloc]init];
    
    praiseListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStyleGrouped];
    praiseListTableView.dataSource=self;
    praiseListTableView.delegate=self;
    praiseListTableView.rowHeight=60;
    praiseListTableView.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
    [self.view addSubview:praiseListTableView];
    
    [self addRefreshAttribute];
    [self getPraiseListWithPage:contentPage andBool:NO];
}

- (void)addRefreshAttribute{
    
    __weak typeof(self) weakSelf = self;
    [praiseListTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:YES]; //刷新
    }];
    [praiseListTableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:NO]; //加载更多
    }];
}

- (void)reloadDataToNew:(BOOL)new{
    
    if (new) {
        contentPage=1;
        [self getPraiseListWithPage:contentPage andBool:YES];
    }else{
        contentPage=contentPage+1;
        [self getPraiseListWithPage:contentPage andBool:NO];
    }
}

-(void)getPraiseListWithPage:(int)page andBool:(BOOL)isRefresh{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_itemid forKey:@"itemid"];
    [CheckParams setObject:_type  forKey:@"type"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [CheckParams setObject:@"10" forKey:@"pagesize"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"get_member_prase.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@">>>>%@",result);
        if (isRefresh) {
            [praiseListTableView.legendHeader endRefreshing];
        }else{
            [praiseListTableView.legendFooter endRefreshing];
        }
        
        if ([[result objectForKey:@"response"] isEqualToString:@"get_member_prase"]) {
            
            NSArray *listArray=[result objectForKey:@"list"];
            if (isRefresh) {
                //下拉刷新
                [praiseListArray removeAllObjects];
            }
            for (NSDictionary *dic in listArray) {
                PraiseInfo *info=[PraiseInfo yy_modelWithDictionary:dic];
                [praiseListArray addObject:info];
            }
        
            if ([[result objectForKey:@"totalcount"] integerValue]==praiseListArray.count) {
                
                [praiseListTableView.legendFooter setState:MJRefreshFooterStateNoMoreData];
            }else{
                [praiseListTableView.legendFooter setState:MJRefreshFooterStateIdle];
            }
            
            [praiseListTableView reloadData];
            
        }else{
            [Config showHUDWithText:@"获取列表失败!" andImage:@"error"];
            return ;
        }
        
    }failureAction:^{
        if (isRefresh) {
            [praiseListTableView.legendHeader endRefreshing];
        }else{
            [praiseListTableView.legendFooter endRefreshing];
        }
        [Config showHUDWithText:@"获取列表失败!" andImage:@"error"];
    }];
}

#pragma mark - UITableView代理

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return praiseListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    PraiseInfo *info=[praiseListArray objectAtIndex:indexPath.row];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 40, 40)];
    headImageView.layer.cornerRadius = 20;
    headImageView.clipsToBounds = YES;
    [headImageView setImageWithURL:[NSURL URLWithString:info.tx_pic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
    [cell addSubview:headImageView];
    
    UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(70, 15, 200 , 30)];
    nameLabel.text=info.truename;
    [cell addSubview:nameLabel];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    PraiseInfo *info=[praiseListArray objectAtIndex:indexPath.row];
    PeopleDataViewController *dataVC=[[PeopleDataViewController alloc]init];
    dataVC.touserid=info.userid;
    [self.navigationController pushViewController:dataVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
